<?php
/**
 * Created by PhpStorm.
 * User: lcz
 * Date: 2018/8/16
 * Time: 16:51
 * 购物车
 */
namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\GoodsRecord;
use think\Config;
use think\Db;

class Cart extends Api {
	
	protected $noNeedRight = ['*'];
	
	
	/**
	 * 购物车列表
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function index(){
		$userId = $this->auth->id;
		$oneAmount = Config::get('oneAmount');
		$returnData = [
			'goods' => [],
			'oneAmount' => $oneAmount,
		];
		
		$returnData['goods'] = Db::name('cart')->alias('c')
			->join('goodsRecord r', 'c.goods_record_id = r.id', 'left')
			->join('goods g', 'r.goods_id = g.id', 'left')
			->where(['r.status' => GoodsRecord::STATUS_B, 'c.user_id' => $userId,'c.status' => \app\common\model\Cart::STATUS_Y])
			->order(['c.create_time' => 'DESC'])
			->field('c.id, g.goods_name, g.image, c.number, r.total_number, r.buy_number')
			->select();
		
		$this->success(__('查询成功'), $returnData);
	}
	
	
	/**
	 * 添加购物车
	 * @param goods_record_id 期数ID
	 * @param number 数量
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function add(){
		$data = input('post.');
		$val = new \app\common\validate\Cart();
		$val->scene('add');
		$res = $val->check($data);
		if(!$res) {
			$this->error($val->getError());
		}
		
		$recordModel = new GoodsRecord();
		$row = $recordModel->find($data['goods_record_id']);
		if(!$row) {
			$this->error(__('记录不存在'));
		}
		
		if($row['status'] != $recordModel::STATUS_B) {
			$this->error(__('该记录已经开奖了'));
		}
		
		$number = $row['total_number'] - $row['buy_number'];
		if($data['number'] > $number){
			$this->error(__('最多购买件', $number));
		}
		
		$userId = $this->auth->id;
		
		$data['user_id'] = $userId;
		$model = new \app\common\model\Cart();
		
		//查找是否已经有这个商品记录，有就商品数量加，否则添加记录
		$cartRow = $model
			->where([
				'user_id' => $userId,
				'goods_record_id' => $data['goods_record_id'],
				'status' => $model::STATUS_Y
			])
			->find();
		
		if($cartRow) {
			$cartRow->setInc('number', $data['number']);
		} else {
			$res = $model->allowField(true)->isUpdate(false)->save($data);
		}
		
		if($res) {
			$this->success(__('添加成功'));
		} else {
			$this->error(__('添加出错').$model->getError());
		}
	}
	
	/**
	 * 编辑购物车
	 * @param id 购物车ID
	 * @param number 数量
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function edit(){
		$data = input('post.');
		$val = new \app\common\validate\Cart();
		$val->scene('edit');
		$res = $val->check($data);
		if(!$res) {
			$this->error($val->getError());
		}
		
		$userId = $this->auth->id;
		
		$model = new \app\common\model\Cart();
		$cartRow = $model->where([
			'id' => $data['id'],
			'user_id' => $userId
		])->find();
		if(!$cartRow) {
			$this->error(__('记录不存在'));
		}
		
		
		$recordModel = new GoodsRecord();
		$row = $recordModel->find($cartRow['goods_record_id']);
		if(!$row) {
			$this->error(__('记录不存在'));
		}
		
		if($row['status'] != $recordModel::STATUS_B) {
			$this->error(__('该记录已经开奖了'));
		}
		
		$number = $row['total_number'] - $row['buy_number'];
		if($data['number'] > $number){
			$this->error(__('最多购买件', $number));
		}
		
		$data['user_id'] = $this->auth->id;
		
		$res = $model->allowField(true)->save($data, ['id' => $data['id'], 'user_id' => $this->auth->id]);
		if($res !== false) {
			$this->success(__('操作成功'));
		} else {
			$this->error(__('操作出错').$model->getError());
		}
	}
	
	
	/**
	 * 删除购物车
	 * @param id 购物车ID
	 */
	public function delete(){
		$userId = $this->auth->id;
		$id = input('post.id');
		$model = new \app\common\model\Cart();
		$res = $model->save(['status' => $model::STATUS_D],[
			'id' => $id,
			'user_id' => $userId
		]);
		
		if($res !== false) {
			$this->success(__('删除成功'));
		} else {
			$this->error(__('操作出错'));
		}
	}
}
